<?php
// +----------------------------------------------------------------------
// | ThinkPHP [ WE CAN DO IT JUST THINK ]
// +----------------------------------------------------------------------
// | Copyright (c) 2006-2016 http://thinkphp.cn All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------
// | Author: 唐轶俊 <liu21st@gmail.com>
// +----------------------------------------------------------------------
/**
 * @return bool|mixed
 * 验证登录，如果登录就返回管理员的ID编号
 */
function is_login(){
    $admin_id = \think\Request::instance()->cookie('blog_admin_id');
    if(!$admin_id){
        return false;
    }
    return $admin_id;
}

/**
 * @param $list
 * @param int $parent_id
 * @param int $deep
 * @return array
 * 递归获取栏目
 */
function infinite($list = [],$parent_id = 0,$deep = 0){
    static $arr = [];
    foreach ($list as $v){
        if($v['p_id'] == $parent_id){
            $v['deep'] = $deep;
            $arr[] = $v;
            infinite($list,$v['id'],$deep + 1);
        }
    }
    return $arr;
}

/**
 * @return string
 * @throws Exception
 * 文件上传
 */
function upload(){
    $file = \think\Request::instance()->file('image');
    $info = $file->validate(['size'=>1567998,'ext'=>'jpg,png,gif,jpeg'])->move(ROOT_PATH . 'public' . DS . 'uploads');
    if(!$info){
        throw new \Exception($file->getError(),\think\Config::get('error_table.upload')['code']);
    }
    $image_path = $info->getSaveName();
    return $image_path;
}

/**
 * 输出时间
 * @param $time
 * @return string
 */
function formatDate($time){
    $difference = time()-$time;
    $date_arr = [
        '31536000'=>'年',
        '2592000'=>'个月',
        '604800'=>'星期',
        '86400'=>'天',
        '3600'=>'小时',
        '60'=>'分钟',
        '1'=>'秒'];
    foreach ($date_arr as $k=>$v)    {
        if (0 != $c = floor($difference/(int)$k)) {
            return $c.$v.'前';
        }
    }
}

